package cn.cloud9.nations;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * @author OnCloud9
 * @description
 * @project RegionReptile
 * @date 2022年07月03日 下午 02:42
 */
public class WorldOrganizationsGet {
    public static void main(String[] args) {
        final Db db = Db.use();
        String url = "https://www.mfa.gov.cn/web/gjhdq_676201/gjhdqzz_681964/";

        final String html = HttpUtil.get(url);
        final Document document = Jsoup.parse(html);
        final Element container = document.getElementsByClass("linkList2").get(0);
        container.getElementsByTag("a").forEach(aTag -> {

            final String ORG_NAME = aTag.text();
            final String href = aTag.attr("href");
            final String replace = href.replace("./", "");
            final String redirectHtml = HttpUtil.get(url + replace);
            final Document redirectDoc = Jsoup.parse(redirectHtml);

            final String data = redirectDoc
                    .getElementsByTag("script")
                    .stream()
                    .filter(script -> script.data().contains("window.location.href"))
                    .findFirst()
                    .get().data();

            final String targetHref = data.substring(data.indexOf("./"), data.length() - 3);
            String TH = url + replace + targetHref;
            final String detailCode = HttpUtil.get(TH);
            final Document detailDoc = Jsoup.parse(detailCode);
            final Element content = detailDoc.getElementById("content");
            if (null == content) return;
            final String text = content.text();
            try {
                db.insert(Entity
                    .create("world_org")
                    .set("NAME", ORG_NAME)
                    .set("LINK", TH)
                    .set("DETAIL", text)
                    .set("GEN_TIME", LocalDateTime.now()));
            } catch (SQLException e) {
                e.printStackTrace();
            }

        });

    }
}
